<?php
namespace app\index\model;
use think\Model;
use think\Db;
use think\Cache;

class KnowUserLog extends Model
{
	public function getUserCount($param, $column = 'sum', $field = 'course.points', $order = 'know_user_log.points desc')
	{
		$user = $param['user'] ?: user();

		return self::field('course.course_id,course.type_id')
						->where('user_id', $user['id'])
						->when($param['nav_id'], function($query) use ($param){
							$query->where('course.nav_id', $param['nav_id']);
						})
						->when($param['type_id'], function($query) use ($param){
							$query->where('course.type_id', $param['type_id']);
						})
						->when($param['s_id'], function($query) use ($param){
							$query->where('course.s_id', $param['s_id']);
						})
						->where('is_show', 1)
						->where('s_show', 1)
						->where(function($query){
							$query->where('status', 1)->whereor('type', 1);
						})
						->join('course', 'course.course_id=know_user_log.course_id')
						->join('specific', 'course.s_id=specific.s_id')
						->group('know_user_log.course_id')
						->order($order)
						->{$column}($field);
	}

	public function getUserData($param = [])
	{
		$user = $param['user'] ?: user();

		return self::field('id,course.course_id,course_name,type,know_user_log.points,origin_points,know_user_log.updated_time')
						->where('user_id', $user['id'])
						->when($param['start'], function($query) use ($param){
							$query->where('know_user_log.updated_time', 'between', [$param['start'], $param['end']]);
						})
						->when($param['type'], function($query) use ($param){
							$query->where('type', $param['type']);
						})
						->where('course.is_show', 1)
						->where('type.is_show', 1)
						->where('nav.is_show', 1)
						->where('s_show', 1)
						->where(function($query){
							$query->where('status', 1)->whereor('type', 1);
						})
						->join('course', 'course.course_id=know_user_log.course_id')
						->join('specific', 'course.s_id=specific.s_id')
						->join('type', 'course.type_id=type.type_id')
						->join('nav', 'course.nav_id=nav.nav_id')
						->order('know_user_log.updated_time desc')
						->paginate($param['size'] ?? 20);
	}


	public function getUserTypeData($param = [])
	{
		$user = $param['user'] ?: user();

		return self::where('user_id', $user['id'])
						->when($param['start'], function($query) use ($param){
							$query->where('know_user_log.updated_time', 'between', [$param['start'], $param['end']]);
						})
						->where('course.is_show', 1)
						->where('type.is_show', 1)
						->where('nav.is_show', 1)
						->where('s_show', 1)
						->where(function($query){
							$query->where('status', 1)->whereor('type', 1);
						})
						->join('course', 'course.course_id=know_user_log.course_id')
						->join('specific', 'course.s_id=specific.s_id')
						->join('type', 'course.type_id=type.type_id')
						->join('nav', 'course.nav_id=nav.nav_id')
						->group('type')
						->column('type,count(*)');
	}


	public function getUserDataOne($param)
	{
		$user = user();

		return self::where('user_id', $user['id'])
						->when($param['id'], function($query) use ($param){
							$query->where('course_id', $param['id']);
						})
						->find();
	}

	public function getUserDataStatusOne($param)
	{
		$user = user();

		return self::where('user_id', $user['id'])
						->where('course_id', $param['id'])
						->find();
	}

	public function getLastData($param)
	{
		$user = user();

		return self::where('user_id', $user['id'])
						->where('course.course_id', $param['id'])
						->where('id','<',$param['log_id'])
						->where('type', $param['type'])
						->where(function($query){
							$query->where('status', 1)->whereor('type', 1);
						})
						->join('course', 'course.course_id=know_user_log.course_id')
						->order('id desc')
						->value('know_user_log.points');

	}


	public function addData($data)
	{
		$user = user();
		self::insert([
			'user_id' => $user['id'],
			'course_id' => $data['id'],
			'status' => $data['status'] ?: 0,
			'points' => $data['points'],
			'origin_points' => $data['origin_points'],
			'created_time' => date('Y-m-d H:i:s'),
			'updated_time' => date('Y-m-d H:i:s'),
		]);
	}

	public function updateData($data)
	{
		$user = user();

		self::where('user_id', $user['id'])
			->where('course_id', $data['id'])
			->update([
				'status' => $data['status'] ?: 0,
				'points' => $data['points'],
				'updated_time' => date('Y-m-d H:i:s'),
			]);
	}
}